<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 预约信息
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.hospitalId" placeholder="医院ID" class="handle-input mr10" @keyup.enter.native="handleSearch"></el-input>
                <el-select v-model="query.appointStatus" placeholder="预约状态" class="handle-select mr10">
                    <el-option key="00" label="已预约" value="00"></el-option>
<!--                    <el-option key="01" label="已使用" value="01"></el-option>-->
                    <el-option key="02" label="取消预约" value="02"></el-option>
                </el-select>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="el-button" icon="el-icon-delete" @click="handleReset">重置</el-button>
            </div>
            <el-table
                    :data="tableData"
                    border
                    class="table"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
                    @selection-change="handleSelectionChange"
            >
                <el-table-column hidden prop="appointId" label="预约ID" width="200" align="center"></el-table-column>
                <el-table-column prop="appointPtId" label="患者ID" width="200" align="center"></el-table-column>
                <el-table-column prop="appointPtNm" label="姓名" align="center" width="100"></el-table-column>
                <el-table-column prop="appointPtPhone" label="手机号" align="center" width="120"></el-table-column>
                <el-table-column prop="appointPreId" label="处方ID" align="center" width="200"></el-table-column>
                <el-table-column prop="ptDoctor" label="主治医生" align="center" width="100"></el-table-column>
                <el-table-column prop="appointStartTm" label="预约时间" align="center" width="160" sortable></el-table-column>
                <el-table-column prop="appointStatusText" label="预约状态" align="center" width="80"></el-table-column>
                <el-table-column prop="hospitalId" label="医院ID" align="center"></el-table-column>
                <el-table-column prop="hospitalSystemId" label="机器编号" align="center"></el-table-column>
<!--                <el-table-column label="操作" width="180" align="center" v-if="roleIds !=='system'">-->
<!--                    <template slot-scope="scope">-->
<!--                        <el-button-->
<!--                                type="text"-->
<!--                                icon="el-icon-edit"-->
<!--                                @click="handleEdit(scope.$index, scope.row)"-->
<!--                        >编辑</el-button>-->
<!--                        <el-button-->
<!--                                type="text"-->
<!--                                icon="el-icon-delete"-->
<!--                                class="red"-->
<!--                                @click="handleDelete(scope.$index, scope.row)"-->
<!--                        >取消预约</el-button>-->
<!--                    </template>-->
<!--                </el-table-column>-->
            </el-table>
            <div class="pagination">
                <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.pageIndex"
                        :page-size="query.pageSize"
                        :total="pageTotal"
                        @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%" :before-close="closeEdit">
            <el-form ref="appointForm" :model="appointForm" :rules="appointFormRules" label-width="100px" >
                <el-form-item hidden label="患者ID" prop="ptId" >
                    <el-input  v-model="appointForm.ptId" style="width: 100%"></el-input>
                </el-form-item>
                <el-form-item label="处方ID" prop="appointPreId">
                    <el-input readonly  v-model="appointForm.appointPreId" style="width: 100%"></el-input>
                </el-form-item>
                <el-form-item label="预约日期" prop="appointStartTm" >
                    <el-date-picker style="width: 80%;"
                                    v-model="appointForm.appointStartTm"
                                    type="date"
                                    placeholder="选择日期"
                                    value-format="yyyy-MM-dd"
                                    :picker-options="pickerOptions">
                    </el-date-picker>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="closeEdit">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import { getAppoints,doDelete,selectByAssessId,doUpdate } from '../../../api/appoint/appoint';
    import {selectPresptByPtId} from "../../../api/patient/patient";
    export default {
        name: 'user',
        data() {
            return {
                query: {
                    hospitalId: '',
                    appointStatus:'00',
                    userAcct:localStorage.getItem('ms_userAcct'),
                    pageIndex: 1,
                    pageSize: 10
                },
                roleIds:localStorage.getItem('ms_roleIds'),
                tableData: [],
                multipleSelection: [],
                editVisible: false,
                pageTotal: 0,
                editForm: {},
                idx: -1,
                id: -1,
                appointForm:{},
                appoints:[],
                appointFormRules: {
                    appointPreId: [
                        { required: true, message: '请选择处方', trigger: 'blur' },
                    ],
                    appointStartTm: [
                        { required: true, message: '请选择预约日期',trigger: 'blur' },
                    ],
                },
                pickerOptions: {
                    disabledDate(time) {
                        return time.getTime() < Date.now() - 8.64e7;
                    }
                }
            };
        },
        created() {
            this.getData();
        },
        methods: {
            // 获取后台数据
            getData() {
                console.log("this.query---",this.query);
                getAppoints(this.query).then(res => {
                    if (res.success == true){
                        this.tableData = res.data;
                        this.pageTotal = res.total;
                    } else {
                        this.$message.error("查询失败！");
                        this.tableData = [];
                        this.pageTotal = 0;
                    }
                });
            },
            // 触发搜索按钮
            handleSearch() {
                this.$set(this.query, 'pageIndex', 1);
                this.getData();
            },
            // 触发重置按钮
            handleReset() {
                this.query.hospitalId = "";
                this.query.appointStatus="00";
                this.getData();
            },
            // 删除操作
            handleDelete(index, row) {
                if (row.appointStatus=="00"){
                    // 二次确认删除
                    this.$confirm('确定要取消吗？', '提示', {
                        type: 'warning'
                    })
                        .then(() => {
                            //后台进行删除操作
                            let condition={
                                appointId:row.appointId
                            };
                            doDelete(condition).then(res => {
                                if (res.data==1){
                                    this.$message.success('取消成功');
                                    this.getData();
                                }else {
                                    this.$message.error('取消失败');
                                    this.getData();
                                }
                            });
                        })
                        .catch(() => {
                            this.$message({
                                message:'已取消操作'
                            });
                        });
                }else{//已使用，或者已取消预约
                    this.$message.error('该预约信息已使用或者已被取消');
                }
            },
            // 多选操作
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 编辑操作
             handleEdit(index, row) {
                this.appointForm={};
                 selectByAssessId({appointId:row.appointId}).then(res => {
                     if (res.success==true){
                         this.appointForm=res.data;
                     }
                 });
                this.editVisible = true;
            },
            closeEdit(){//关闭编辑弹出框
                this.$confirm('确认关闭？', '提示', {
                    type: 'warning'
                })
                    .then(() => {
                        this.editVisible=false;
                        this.appointForm={};
                        this.getData();
                    })
                    .catch(() => {
                        this.$message({
                            message:'已取消操作'
                        });
                    });
            },
            // 保存编辑
            saveEdit() {
                this.$refs['appointForm'].validate((valid) => {
                    if (valid) {
                        console.log("this.appointForm---",this.appointForm);
                        doUpdate(this.appointForm).then(res => {
                            if (res.data == 1) {
                                this.editVisible = false;
                                this.appointForm = {};
                                this.$message.success('编辑成功');
                                this.getData();
                            } else {
                                this.$message.error('编辑失败');
                            }
                        });
                    }
                });
            },
            // 分页导航
            handlePageChange(val) {
                this.$set(this.query, 'pageIndex', val);
                this.getData();
            },
        }
    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .table {
        width: 100%;
        font-size: 14px;
    }
    .red {
        color: #ff0000;
    }
    .mr10 {
        margin-right: 10px;
    }
    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
        /*border-radius 属性允许您为元素添加圆角边框！*/
        border-radius:6px;
        background-clip:padding-box;
        border-top-left-radius: 6px;
        border-top-right-radius: 6px;
        border-bottom-right-radius: 6px;
        border-bottom-left-radius: 6px;
    }
    /*头像上传*/
    .user-header{
        position: relative;
        display: inline-block;
    }
    .user-header-com{
        width: 100px;
        height: 100px;
        border-radius: 50%;
        display: inline-block;
    }
    .header-upload-btn{
        position: absolute;
        left: 0;
        top: 0;
        opacity: 0;
        /* 通过定位把input放在img标签上面，通过不透明度隐藏 */
    }
</style>
